<?php
/**
 * 管理组模型
 * @author hedong <2015-12-07 16:31:45>
 */
namespace OrderAdmin\Model;

use Carbon\Carbon;
use Think\Model\RelationModel;

class PartnerModel extends RelationModel {

    /**
     * 分页查询
     * @param $param
     * @return array
     */
    function getData($param){
        // 请求参数
        if($param['key']){
            $where['a.name'] = ['like', '%'.$param['key'].'%'];
            $where['a.mobile'] = ['like', '%'.$param['key'].'%'];
            $where['a.company_name'] = ['like', '%'.$param['key'].'%'];
        }
        $where2 = [];
        if($where && $where2){
            $where['_logic'] = 'or';
            $where2['_complex'] = $where;
        }elseif($where && !$where2){
            $where['_logic'] = 'or';
            $where2 = $where;
        }elseif(!$where && !$where2){
            $where2 = [];
        }

        // 获取数据
        $pageNum = ($param['page'] - 1) * $param['size'];
        $data['count'] = $this->alias('a')
            ->where($where2)->count();
        if($data['count'] < $pageNum){
            return ['status' => false, 'msg'=>'没有更多数据了'];
        }
        $data['data'] = $this->alias('a')
            ->field('a.*')
            ->where($where2)
            ->order('a.add_time desc')
            ->limit($pageNum, $param['size'])
            ->select();

        foreach ($data['data'] as &$v){
            $v['worker_count'] = M('worker')->where(['partner_id'=>$v['id']])->count();
        }
        unset($v);
        return ['status'=> true, 'data'=> $data];
    }

    /**
     * 获取当月新增数
     * @return mixed
     */
    public function getThisMonthCount(){
        $startTime = Carbon::now()->firstOfMonth()->toDateTimeString();
        $endTime = Carbon::now()->firstOfMonth()->addMonth(1)->toDateTimeString();
        return $this->where(['add_time'=>['between', [$startTime, $endTime]]])->count();
    }

    /**
     * 合伙人详情
     * @param $id
     * @return bool
     */
    public function getInfoById($id){
        $data = $this->alias('a')
            ->field('a.id, a.status, a.name, a.mobile, a.area, a.cert_id, a.invite_code, a.add_time, a.company_name,
             a.corporation_name, a.province, a.city, a.area, a.address, concat_ws("", a.province, a.city, a.area, a.address) detail_address,
             b.filepath business_license, c.filepath contract, d.filepath certid_front, e.filepath certid_side, f.filepath headimg')
            ->join('LEFT JOIN __FILES__ b ON a.business_license = b.file_id')
            ->join('LEFT JOIN __FILES__ c ON a.contract = c.file_id')
            ->join('LEFT JOIN __FILES__ d ON a.certid_front = d.file_id')
            ->join('LEFT JOIN __FILES__ e ON a.certid_side = e.file_id')
            ->join('LEFT JOIN __FILES__ f ON a.headimg = f.file_id')
            ->where(['id'=>$id])
            ->find();
        $data['full_business_license'] = makeImagePath($data['business_license']);
        $data['full_contract'] = makeImagePath($data['contract']);
        $data['full_certid_front'] = makeImagePath($data['certid_front']);
        $data['full_certid_side'] = makeImagePath($data['certid_side']);
        $data['full_headimg'] = makeImagePath($data['headimg']);
        return $data ? : false;
    }
}
